#!/bin/bash
set -Euo pipefail

RED='\e[1;31m'
GREEN='\e[1;32m'
CYAN='\e[1;36m'
NC='\e[0m' # 没有颜色

RISTRICT_WORD="^rm | rm |^mv | mv "

if [[ $# -eq 1 ]] && [[ "$1" = "-v" ]]
then
    VIEW=1
else
    VIEW=0
fi

# 颜色打印函数，输入参数2个 cl_print title|ok|fail  msg
# title是CYAN的颜色，ok 绿色  fail是红色
function cl_print
{

 tp=$1
 msg=$2
 if [[ ${tp} == 'title' ]]
 then
   echo -e "${CYAN}----${msg}${NC}"
 elif [[ ${tp} == 'ok' ]]
 then
   echo -e "${GREEN}----${msg} result: ok${NC}"
 elif [[ ${tp} == 'fail' ]]
 then
   echo -e "${RED}----${msg} result: fail${NC}"
 elif [[ ${tp} == 'red' ]]
 then
   echo -e "${RED}${msg}${NC}"
 elif [[ ${tp} == 'green' ]]
 then
   echo -e "${GREEN}${msg}${NC}"
 else
   echo -e "${NC}----${msg} ${NC}"
 fi

}

# 初始化变量，循环时保证安全，先初始化变量
function init
{
    name=''
    cmd=''
    check=''
}

# 检查功能函数
function check
{
    check_operator=$1
    cmd=$2
    check_status=$3
    name=$4

    if [ "$check_operator" = "=" ]
    then
       if [ "$cmd" = "$check_status" ]
       then
           cl_print ok "$name"
       else
           cl_print fail  "$name"
       fi

    elif [ "$check_operator" = "!=" ]
    then
       if [ "$cmd" != "$check_status" ]
       then
           cl_print ok "$name"
       else
           cl_print fail  "$name"
       fi

    elif [ "$check_operator" = "-eq" ]
    then
       if [ "$cmd" -eq "$check_status" ]
       then
           cl_print ok "$name"
       else
           cl_print fail  "$name"
       fi

    elif [ "$check_operator" = "-gt" ]
    then
       if [ "$cmd" -gt "$check_status" ]
       then
           cl_print ok "$name"
       else
           cl_print fail  "$name"
       fi

    elif [ "$check_operator" = "-lt" ]
    then
       if [ "$cmd" -lt "$check_status" ]
       then
           cl_print ok "$name"
       else
           cl_print fail  "$name"
       fi

    elif [ "$check_operator" = "-ge" ]
    then
       if [ "$cmd" -ge "$check_status" ]
       then
           cl_print ok "$name"
       else
           cl_print fail  "$name"
       fi

    elif [ "$check_operator" = "-le" ]
    then
       if [ "$cmd" -le "$check_status" ]
       then
           cl_print ok "$name"
       else
           cl_print fail  "$name"
       fi
    
    else
       cl_print red "check operator error, it must be one of (!= = -eq -gt -lt -ge -le)"
       return 1
    fi
}

# 检查cmd是否有危险命令
function restict
{
  if [[ $(echo $1 | grep "${RISTRICT_WORD}" | wc -l) -eq 1 ]]
  then
    cl_print red "cmd中有受限字符${RISTRICT_WORD}"
    return 1
  fi 
  
}

# 主函数
function main
{
    for f in $(ls conf_inspect/check*.sh)
    do
        # 初始化变量
        init
        # 获取新的配置,新的配置以变量的形式影响后面的运行
        source ${f}
        # 打印标签
        cl_print title "$name"
        
        # 检查是否有受限命令
        restict "$cmd"
        if [ $? -ne 0 ]
        then
          continue
        fi

        # 运行cmd命令
        cmd_rst=$(eval $cmd)

        # 有-v选项 则打印cmd_rst
        if [[ ${VIEW} -eq 1 ]]
        then
           echo -e "$cmd_rst"
        fi

        check_operator=$(echo $check | awk -F, '{print $1}')
        check_status=$(echo $check | awk -F, '{print $2}')
        check_cmd=$(echo $check | awk -F, '{print $3}')

        if [[ -z "$cmd_rst" ]]
        then
          result=$(eval $cmd | $check_cmd)
        else
          if [[ -z "$check_cmd" ]]
          then
            result=$(echo -e "$cmd_rst")
          else
            result=$(echo -e "$cmd_rst" | eval $check_cmd)
          fi
        fi
        
        check "$check_operator" "$result" "$check_status" "$name"
        
        echo
    done

}

main

